package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.commonlib.bean.UpdateVersionInfo;
import com.lanlian.commonlib.dao.DBManager;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2019/1/21 19:03
 *     desc   : 版本更新对话框
 *     version: 1.0
 * </pre>
 */
public class UpdateVersionDialog extends Dialog implements View.OnClickListener {

    private ImageView mImgClose;
    private LinearLayout mLayUpdate;
    private TextView mTvClose;
    private TextView mTvUpdate;
    private LinearLayout mLayFocusUpdate;
    private TextView mTvFocusUpdate;
    private TextView mTvDesc;

    private Context mContext;
    private UpdateVersionInfo mUpdateVersion;

    public UpdateVersionDialog(@NonNull Context context, UpdateVersionInfo updateVersionInfo) {
        super(context, R.style.customDialog1);
        this.mContext = context;
        this.mUpdateVersion = updateVersionInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_version);
        initView();
        setListeners();
        setCanceledOnTouchOutside(false);
        if ("1".equals(mUpdateVersion.getForceUpdate())) {
            mImgClose.setVisibility(View.GONE);
            mLayUpdate.setVisibility(View.GONE);
            mLayFocusUpdate.setVisibility(View.VISIBLE);
        } else {
            mImgClose.setVisibility(View.VISIBLE);
            mLayUpdate.setVisibility(View.VISIBLE);
            mLayFocusUpdate.setVisibility(View.GONE);
        }
        mTvDesc.setText(String.format("版本有更新，推荐下载最新版本%s使用", mUpdateVersion.getVersionName()));
    }

    private void initView() {
        mImgClose = findViewById(R.id.img_close);
        mLayUpdate = findViewById(R.id.lay_update);
        mTvClose = findViewById(R.id.tv_close);
        mTvUpdate = findViewById(R.id.tv_update);
        mLayFocusUpdate = findViewById(R.id.lay_focus_update);
        mTvFocusUpdate = findViewById(R.id.tv_focus_update);
        mTvDesc = findViewById(R.id.tv_desc);
    }

    private void setListeners() {
        mImgClose.setOnClickListener(this);
        mTvClose.setOnClickListener(this);
        mTvUpdate.setOnClickListener(this);
        mTvFocusUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_close:
                dismiss();
                break;
            case R.id.tv_close:
                DBManager.getInstance().setTempVersionCode(mUpdateVersion.getVersionNo());
                dismiss();
                break;
            case R.id.tv_update:
                update(mUpdateVersion.getUrl());
                break;
            case R.id.tv_focus_update:
                update(mUpdateVersion.getUrl());
                break;
            default:
                break;
        }
    }

    private void update(String url) {
        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_VIEW);
        mContext.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if (isShowing()) {
            return;
        }
        super.onBackPressed();
    }

}
